package servlet.user;



import domain.User;
import service.user.UserService;
import service.user.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserLogin extends HttpServlet {
    private static final long serialVersionUID = 1L;
    UserService userService = new UserServiceImpl();
    public UserLogin() {
        super();
        // TODO Auto-generated constructor stub
    }



    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String un = request.getParameter("username");
        String pw = request.getParameter("password");

        String msg = null;
        User user = userService.verifyUser(un,pw);
        if (user.getUsername() != null) {
            request.getSession().setAttribute("userloginflag", user);
            response.sendRedirect(request.getContextPath() + "/admin/index.jsp");
        } else {
            System.out.println("程序执行至此处，无对应用户名密码");
            request.setAttribute("loginmsg","error");
            response.sendRedirect(request.getContextPath() + "/admin/login.html");
        }
    }
}
